package ar.edu.utn.frsf.tpae.a2012.g05.entity;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Random;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "cli")
public class Cliente {

	@Id
	@Column(name = "cli_id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Column(name = "cli_nro", length = 48, nullable = false, unique = true)
	private String numero;

	@Column(name = "cli_nom", length = 48, nullable = false)
	private String nombre;

	@Column(name = "cli_ape", length = 48, nullable = false)
	private String apellido;

	@Column(name = "cli_dni", length = 10, nullable = false)
	private String documento;

	@Column(name = "cli_cuil", length = 16, nullable = false)
	private String cuil;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "cli_fech_alta", nullable = false)
	private Date fechaAlta;

	@Column(name = "cli_dir_pos", length = 48, nullable = false)
	private String direccionPostal;

	@Column(name = "cli_tel", length = 20)
	private String telefono;

	@Column(name = "cli_mail", length = 45)
	private String email;

	public Long getId() {
		return id;
	}

	public Cliente(boolean b) {
		super();
		if (b) {
			this.id = null;
			this.numero = new BigDecimal(new Random().nextInt()).abs().toString();
			this.nombre = "Manuel";
			this.apellido = "Pretto";
			this.documento = "11111111";
			this.cuil = "20-1111111-2";
			this.fechaAlta = new Date();
			this.direccionPostal = "3000";
			this.telefono = "4905123";
			this.email = "mail@mail.com";
		}
	}
	
	public Cliente() {
		super();
	}

	public Cliente(String numero, String nombre, String apellido,
			String documento, String cuil, Date fechaAlta,
			String direccionPostal, String telefono, String email) {
		super();
		this.numero = numero;
		this.nombre = nombre;
		this.apellido = apellido;
		this.documento = documento;
		this.cuil = cuil;
		this.fechaAlta = fechaAlta;
		this.direccionPostal = direccionPostal;
		this.telefono = telefono;
		this.email = email;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getNumero() {
		return numero;
	}

	public void setNumero(String numero) {
		this.numero = numero;
	}

	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public String getApellido() {
		return apellido;
	}

	public void setApellido(String apellido) {
		this.apellido = apellido;
	}

	public String getDocumento() {
		return documento;
	}

	public void setDocumento(String documento) {
		this.documento = documento;
	}

	public String getCuil() {
		return cuil;
	}

	public void setCuil(String cuil) {
		this.cuil = cuil;
	}

	public Date getFechaAlta() {
		return fechaAlta;
	}

	public void setFechaAlta(Date fechaAlta) {
		this.fechaAlta = fechaAlta;
	}

	public String getDireccionPostal() {
		return direccionPostal;
	}

	public void setDireccionPostal(String direccionPostal) {
		this.direccionPostal = direccionPostal;
	}

	public String getTelefono() {
		return telefono;
	}

	public void setTelefono(String telefono) {
		this.telefono = telefono;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;

		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((numero == null) ? 0 : numero.hashCode());
		result = prime * result + ((nombre == null) ? 0 : nombre.hashCode());
		result = prime * result
				+ ((apellido == null) ? 0 : apellido.hashCode());
		result = prime * result
				+ ((documento == null) ? 0 : documento.hashCode());
		result = prime * result + ((cuil == null) ? 0 : cuil.hashCode());
		result = prime * result
				+ ((fechaAlta == null) ? 0 : fechaAlta.hashCode());
		result = prime * result
				+ ((direccionPostal == null) ? 0 : direccionPostal.hashCode());
		result = prime * result
				+ ((telefono == null) ? 0 : telefono.hashCode());
		result = prime * result + ((email == null) ? 0 : email.hashCode());

		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Cliente)) {
			return false;
		}

		Cliente other = (Cliente) obj;

		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}

		if (nombre == null) {
			if (other.nombre != null) {
				return false;
			}
		} else if (!nombre.equals(other.nombre)) {
			return false;
		}

		if (apellido == null) {
			if (other.apellido != null) {
				return false;
			}
		} else if (!apellido.equals(other.apellido)) {
			return false;
		}

		if (documento == null) {
			if (other.documento != null) {
				return false;
			}
		} else if (!documento.equals(other.documento)) {
			return false;
		}

		if (cuil == null) {
			if (other.cuil != null) {
				return false;
			}
		} else if (!cuil.equals(other.cuil)) {
			return false;
		}

		if (fechaAlta == null) {
			if (other.fechaAlta != null) {
				return false;
			}
		} else if (!fechaAlta.equals(other.fechaAlta)) {
			return false;
		}

		if (direccionPostal == null) {
			if (other.direccionPostal != null) {
				return false;
			}
		} else if (!direccionPostal.equals(other.direccionPostal)) {
			return false;
		}

		if (telefono == null) {
			if (other.telefono != null) {
				return false;
			}
		} else if (!telefono.equals(other.telefono)) {
			return false;
		}

		if (email == null) {
			if (other.email != null) {
				return false;
			}
		} else if (!email.equals(other.email)) {
			return false;
		}

		return true;
	}

	@Override
	public String toString() {
		return toFullString();
	}
	
	public String toFullString() {
		StringBuilder result = new StringBuilder();
		String newLine = System.getProperty("line.separator");

		result.append(this.getClass().getName() + "{");
		result.append(newLine);

		Field[] fields = this.getClass().getDeclaredFields();

		for (Field field : fields) {
			result.append("  ");
			try {
				result.append(field.getName());
				result.append(": ");
				result.append(field.get(this));
			} catch (IllegalAccessException ex) {
				System.out.println(ex);
			}
			result.append(newLine);
		}
		result.append("}");

		return result.toString();
	}

}
// cli_id integer NOT NULL DEFAULT nextval('seq_cli'::regclass),
// cli_nro character varying(16) NOT NULL,
// cli_nom character varying(48) NOT NULL,
// cli_ape character varying(48) NOT NULL,
// cli_dni character varying(10) NOT NULL,
// cli_cuil character varying(16) NOT NULL,
// cli_fech_alta timestamp without time zone NOT NULL,
// cli_dir_pos character varying(48) NOT NULL,
// cli_tel character varying(20),
// cli_mail character varying(48),
// CONSTRAINT cli_pk PRIMARY KEY (cli_id),
// CONSTRAINT cli_uidx_nro UNIQUE (cli_nro)